<?php

namespace App\Admin\Controller\Base;

use App\AbstractController;
use App\Admin\Service\Base\AdministratorsService;
use App\Admin\Service\Base\DefaultLoginService;
use App\Admin\Validate\Base\AdministratorsValidate;
use App\Common\Exception\ApiMessageException;
use App\Common\Model\Model;
use App\Common\Util\Response;
use App\Common\Util\TP;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\Redirect;

/**
 * 管理员控制器
 */
class AdministratorsController extends AbstractController
{

    /**
     * 列表页
     *
     * @return string
     */
    public function lists(): string
    {
        return Response::render('Base/Administrators/lists');
    }

    /**
     * 列表数据
     *
     * @return \think\Response
     * @throws \Exception
     */
    public function listsData(): \think\Response
    {
        return Response::json()->success(AdministratorsService::aop()->lists());
    }

    /**
     * 创建数据页面
     *
     */
    public function create(): string
    {
        return Response::render('Base/Administrators/form');
    }

    /**
     * @param AdministratorsValidate $validate
     *
     * @return \think\Response
     * @throws ApiMessageException
     */
    public function store(AdministratorsValidate $validate): \think\Response
    {
        AdministratorsService::aop()->store($validate->post());

        return Response::json()->success();
    }

    /**
     * 更新数据页面
     *
     * @return string
     */
    public function edit()
    {
        return Response::render('Base/Administrators/form');
    }

    /**
     * 更新数据
     *
     * @param AdministratorsValidate $validate
     *
     * @return \think\Response
     * @throws ApiMessageException
     */
    public function update(AdministratorsValidate $validate): \think\Response
    {
        AdministratorsService::aop()->update($validate->data());

        return Response::json()->success();
    }

    /**
     * 删除数据
     *
     * @param AdministratorsValidate $validate
     * @return \think\Response
     * @throws ApiMessageException
     */
    public function destroy(AdministratorsValidate $validate): \think\Response
    {
        AdministratorsService::aop()->destroy($validate->input('ids'));

        return Response::json()->success();
    }

    /**
     * 开关切换数值更新
     *
     * @return \think\Response
     * @throws ApiMessageException
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function switchToggle(): \think\Response
    {
        AdministratorsService::aop()->toggle($this->request->post());

        return Response::json()->success();
    }


    /**
     * 重置密码
     *
     * @return \think\Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function reseatPassword(): \think\Response
    {
        $id = $this->request->post('id');

        AdministratorsService::aop()->reseatPassword($id);

        return Response::json()->success();
    }

    /**
     * 登录账号
     *
     * @param DefaultLoginService $defaultLoginService
     *
     * @return \think\Response|Redirect
     * @throws \Exception
     */
    public function loginAccount(DefaultLoginService $defaultLoginService): \think\Response|Redirect
    {
        $id = $this->request->post('id');

        $defaultLoginService->gotoAccount((int)$id);

        return Response::json()->success();
    }
}
